package com.juzipi.controller;


import com.juzipi.common.constant.Constants;
import com.juzipi.common.constant.json.ResultCode;
import com.juzipi.common.model.LoginBody;
import com.juzipi.common.tools.Result;
import com.juzipi.common.tools.ResultTool;
import com.juzipi.common.utils.ServerUtils;
import com.juzipi.common.utils.StringUtils;
import com.juzipi.common.utils.security.SecurityUtils;
import com.juzipi.framework.web.service.TokenService;
import com.juzipi.inter.domain.LoginUser;
import com.juzipi.service.impl.UserAccountService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Author juzipi
 * @Date 2021/4/12 16:46
 * @Info 用户账户相关
 */

@RestController
@RequestMapping("account")
public class UserAccountController extends UserController {


    @Autowired
    private UserAccountService userAccountService;
    @Autowired
    private TokenService tokenService;


    /**
     * 注册
     * @param loginBody
     * @return Result
     */
    @PutMapping("register")
    @ApiOperation("注册")
    public Result registerUser(@RequestBody LoginBody loginBody) {
        Object object = userAccountService.registerUser(loginBody);
        return checkUniqueResult(object);
    }


    /**
     * 通过邮箱获取注册用户的验证码
     * @param email
     * @return
     */
    @PostMapping("getRegisterCode")
    @ApiOperation("验证邮箱获取验证码注册")
    public Result registerCode(@RequestParam(value = "email") String email) {
        if (userAccountService.registerCode(email)){
            return ResultTool.successCustomize(ResultCode.SUCCESS,"已发送");
        }
        return ResultTool.failCustomize(ResultCode.FAIL,email);
    }



    /**
     * 验证邮箱验证码是否匹配
     * @param email 邮箱
     * @param code 验证码
     * @return Json
     */
    @PostMapping("checkEmailAndCode")
    @ApiOperation("根据验证邮箱验证验证码")
    public Result checkEmailAndCode(@RequestParam(value = "email")String email,@RequestParam(value = "code")String code) {
        return JudgeBooleanResult(userAccountService.checkEmailAndCode(email, code));
    }



    /**
     * 修改密码
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return Json结果
     */
    @PreAuthorize("hasAnyRole('user','admin','super')")
    @PutMapping("setPwd")
    @ApiOperation("修改密码")
    public Result modifyPassword(@RequestParam String oldPassword, @RequestParam String newPassword) {
        //获取到登录的用户
        LoginUser loginUser = tokenService.getLoginUser(ServerUtils.getRequest());
        String username = loginUser.getUsername();
        String password = loginUser.getPassword();
        //比较原密码是否与数据库中的密码相同
        if (!SecurityUtils.matchesPassword(oldPassword, password)) {
            return ResultTool.failCustomize(ResultCode.FAIL,"原密码错误");
        }
        //判断新密码是否与原密码相同
        if (SecurityUtils.matchesPassword(newPassword, password)) {
            return ResultTool.failCustomize(ResultCode.FAIL,"新密码不能与原密码相同");
        }
        Integer affect = userAccountService.modifyPassword(username, SecurityUtils.encryptPassword(newPassword));
        if (affect > 0) {
            //删除用户的登录信息，重新登录
            tokenService.delLoginUser(loginUser.getUuid());
            return ResultTool.successCustomize(ResultCode.MODIFY_SUCCESSFULLY,"密码修改成功，请重新登录");
        }
        return ResultTool.failCustomize(ResultCode.MODIFY_FAIL);
    }



    @PostMapping("sendCode")
    @ApiOperation("验证邮箱并发送验证码,找回密码用")
    public Result sendCode(@RequestParam String username, @RequestParam String email) {
        if (userAccountService.sendCode(username,email)) {
            return ResultTool.successCustomize(ResultCode.SUCCESS,"邮件已发送");
        }
        return ResultTool.failCustomize(ResultCode.FAIL,"邮箱错误");
    }



    @PostMapping("checkCode")
    @ApiOperation("验证通过并登录,找回密码用")
    public Result checkCode(@RequestParam String username, @RequestParam String emailCode) {
        String message = userAccountService.checkCode(username, emailCode);
        if (StringUtils.isNotEmpty(message)) {
            if (Objects.equals(message,Constants.CODE_EXPIRED)) {
                return ResultTool.failCustomize(ResultCode.FAIL,"验证码已过期");
            }
            return ResultTool.successCustomize(ResultCode.USER_PASSWORD_CHANGE_CODE,message);
        }
        return ResultTool.failCustomize(ResultCode.FAIL,"验证失败");
    }

}
